mini.parse();

function GoodsTrashHandler() {
};

GoodsTrashHandler.instance = new GoodsTrashHandler();

GoodsTrashHandler.isQueryBtnExpended = false;

//获得全局的商品数据表格对象
GoodsTrashHandler.goodsTrashGrid = mini.get("goodsTrashGrid");
//加载表格数据
GoodsTrashHandler.goodsTrashGrid.load();

/**
 * 根据用户权限展示功能按钮
 */
GoodsTrashHandler.loadToolbar = function() {
	var array = new Array("goods_trash_revert", "goods_trash_del","goods_trash_query","goods_trash_refresh");
	var userArr = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < userArr.length; i++) {
		mini.get(array[i]).setVisible(userArr[i]);
	}
};
GoodsTrashHandler.loadToolbar();

/**
 * 批量还原商品
 */
GoodsTrashHandler.revertGoods = function(){
	var rowsData = GoodsTrashHandler.goodsTrashGrid.getSelecteds();
	if(rowsData.length > 0){
		mini.confirm("确认还原选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bgiId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bgiId;
					
					GoodsTrashHandler.goodsTrashGrid.loading();
                    $.ajax({
                        url: __root + "/revertGoodsByIds.do",
                        data: {
				        	bgiIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                        	MiniuiTools.miniInfo("系统提示","操作成功！", function(){
				        		GoodsTrashHandler.goodsTrashGrid.reload();
				        	});
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
            				MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
                        },
                        complete: function(){
                        	GoodsTrashHandler.goodsTrashGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		MiniuiTools.miniWarning("系统提示","请选择要还原的商品资源！");
	}
}

/**
 * 还原单个商品
 */
GoodsTrashHandler.updateGoodsStateById = function() {
	if (!MiniuiTools.hasPurview("goods_trash_revert")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}	
	mini.confirm("确认还原该商品？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var rowData = GoodsTrashHandler.goodsTrashGrid.getSelected();
					var bgiId = rowData.bgiId;					
					GoodsTrashHandler.goodsTrashGrid.loading();
					
				    $.ajax({
				        url: __root + "/updateGoodsStateById.do",
				        data: {
				        	bgiId: bgiId,
				        	isRevert: "isRevert",
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
				        success: function (text) {
				            GoodsTrashHandler.goodsTrashGrid.reload();
				        },
				        error: function (jqXHR, textStatus, errorThrown) {
            				MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
				        },
                        complete: function(){
                        	GoodsTrashHandler.goodsTrashGrid.unmask();
                        }
				    });	
				} 
			}
		)
};

/**
 * 批量删除商品
 */
GoodsTrashHandler.delGoods = function(){
	var rowsData = GoodsTrashHandler.goodsTrashGrid.getSelecteds();
	if(rowsData.length > 0){
		mini.confirm("确认删除选择的" + rowsData.length + "条记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var ids = "";
					for ( var i = 0; i < rowsData.length - 1; i++){
						ids += rowsData[i].bgiId+ ",";
					}
					ids += rowsData[rowsData.length - 1].bgiId;
					
					GoodsTrashHandler.goodsTrashGrid.loading();
                    $.ajax({
                        url: __root + "/delGoodsByIds.do",
                        data: {
				        	bgiIds: ids,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
                        success: function (text) {
                            GoodsTrashHandler.goodsTrashGrid.reload();
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                        	mini.alert("操作失败！" + jqXHR.responseText,"系统提示",null);
                        },
                        complete: function(){
                        	GoodsTrashHandler.goodsTrashGrid.unmask();
                        }
                    });	
                } 
			}
		)
	}else{
		mini.alert("请选择要删除的资源！","系统提示",null);
	}
}

/**
 * 删除单个商品
 */
GoodsTrashHandler.delGoodsById = function() {
	if (!MiniuiTools.hasPurview("goods_trash_del")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}	
	mini.confirm("确认删除该商品及属性记录？" ,"系统提示",
			function(action, value){
				if (action == "ok") {
					var rowData = GoodsTrashHandler.goodsTrashGrid.getSelected();
					var bgiId = rowData.bgiId;					
					GoodsTrashHandler.goodsTrashGrid.loading();
					
				    $.ajax({
				        url: __root + "/delGoodsByIds.do",
				        data: {
				        	bgiIds: bgiId,
				        	lslUser: gbOperateUser,
				    		lslAddress: gbOperateIp
				        },
				        success: function (text) {
				        	MiniuiTools.miniInfo("系统提示","操作成功！", function(){
				        		GoodsTrashHandler.goodsTrashGrid.reload();
				        	});
				        },
				        error: function (jqXHR, textStatus, errorThrown) {
            				MiniuiTools.miniError("系统提示","操作失败！" + jqXHR.responseText);
				        },
                        complete: function(){
                        	GoodsTrashHandler.goodsTrashGrid.unmask();
                        }
				    });	
				} 
			}
		)
};

/**
 * 展开或折叠查询商品面板
 */
GoodsTrashHandler.queryGoodsTrash = function() {
	if (GoodsTrashHandler.isQueryBtnExpended) {
		GoodsTrashHandler.isQueryBtnExpended = false;
		mini.get("goods_trash_query").setText("展开查询");
		mini.get("goods_trash_query").setIconCls("icon-btn-expand");
	} else {
		GoodsTrashHandler.isQueryBtnExpended = true;
		mini.get("goods_trash_query").setText("折叠查询");
		mini.get("goods_trash_query").setIconCls("icon-btn-collapse");
	}
	
	$("#queryGoodsTrashDiv").toggle(500);
    setTimeout(function(){mini.get("goodsTrashPanel").doLayout();},600);
};

/**
 * 执行查询商品的方法
 */
GoodsTrashHandler.doQueryGoodsTrash = function(){
	var bgiKeyword = mini.get("bgiKeyword").getValue();
	
    GoodsTrashHandler.goodsTrashGrid.load({bgiKeyword: bgiKeyword});
};

/**
 * 刷新页面数据及布局
 */
GoodsTrashHandler.afresh = function(){
	GoodsTrashHandler.doQueryGoodsTrash();
	mini.get("goodsTrashPanel").doLayout();
}

/**
 * DrawCell 绘制单元格
 */
GoodsTrashHandler.goodsTrashGrid.on("drawcell", function(e) {
	 var record = e.record;
     var column = e.column;
     var field = e.field;
     var value = e.value;

     if (column.field == "bgiState") {
        if (e.value == 1) {
            e.cellHtml = "<span class='icon-show-yes' style='cursor: pointer;' onclick='GoodsTrashHandler.updateGoodsStateById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";                    
        } else {
            e.cellHtml = "<span class='icon-show-no' style='cursor: pointer;' onclick='GoodsTrashHandler.updateGoodsStateById()'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>";
        }
    }
    
    if(column.field == "bgiOperation"){
    	e.cellHtml = "<span><a style='cursor: pointer;' onclick='GoodsTrashHandler.updateGoodsStateById()'>还原</a></span>"
    			   + "&nbsp;|&nbsp;"	
    			   + "<span><a style='cursor: pointer;' onclick='GoodsTrashHandler.delGoodsById()'>移除</a></span>";
    }
});

